﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Sale_Model;
using Sale_Common;
using Sale_DAL;
using System.Web;
namespace Sale_Operation
{
    public class SysUsersOp
    {
        /// <summary>
        /// 如果登陆成功，并返回用户的所有信息
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public bool Login(  SysUser user)
        {
            bool blRes= false;
            StringBuilder sbSql = new StringBuilder();
            sbSql.Append(" SELECT SUser_Id,SUser_Name,SUser_Pwd,State,CreateTime,CreateMan FROM SysUser WHERE State=1 ");
            sbSql.Append(" AND SUser_Name='"+ValueHandler.GetStringValue(user.SUser_Name, ValueHandler.CharHandlerType.IsRepChar)+"' ");
            sbSql.Append(" AND SUser_Pwd='" + ValueHandler.GetStringValue(user.SUser_Pwd, ValueHandler.CharHandlerType.IsRepChar) + "' ");
            DataBase db = new DataBase();
            SqlHelper.FillDataset(db.Connection, System.Data.CommandType.Text, sbSql.ToString(), db.Data, new string[] { "dtSysUser" });
            if (db.Data.Tables.Count > 0)
            {
                if (db.Data.Tables["dtSysUser"].Rows.Count > 0)
                {
                    user.CreateMan = ValueHandler.GetIntNumberValue(db.Data.Tables["dtSysUser"].Rows[0]["CreateMan"]);
                    user.CreateTime = DateTime.Parse(ValueHandler.GetStringDateValue(db.Data.Tables["dtSysUser"].Rows[0]["CreateTime"]));
                    user.State = ValueHandler.GetIntNumberValue(db.Data.Tables["dtSysUser"].Rows[0]["State"]);
                    user.SUser_Id = ValueHandler.GetIntNumberValue(db.Data.Tables["dtSysUser"].Rows[0]["SUser_Id"]);
                    blRes = true;
                }
            }
            return blRes;
        }
        /// <summary>
        /// 获取所有用户
        /// </summary>
        /// <returns></returns>
        public DataTable GetList(string strCondition)
        {
            DataBase db = new DataBase();
            try
            {
                StringBuilder sbSql = new StringBuilder();
                sbSql.Append(" SELECT SUser_Id,SUser_Name,SUser_Pwd,State,CreateTime,CreateMan FROM SysUser WHERE State=1");
                if (!string.IsNullOrEmpty(strCondition))
                    sbSql.Append(" And " + strCondition);
                SqlHelper.FillDataset(db.Connection, System.Data.CommandType.Text, sbSql.ToString(), db.Data, new string[] { "dtList" });
            }
            catch (Exception ex)
            { 
            }
            db.Close();
            return db.Data.Tables[0];
        }
        public bool Save(SysUser user)
        {
            DataBase db = new DataBase();
            bool blRes = false;
            try
            {
                StringBuilder sbSql = new StringBuilder();
                if (user.SUser_Id == 0)
                {
                    sbSql.Append(" INSERT INTO SysUser (SUser_Name,SUser_Pwd,State,CreateTime,CreateMan) SELECT  ");
                    sbSql.Append(" SUser_Name='" + ValueHandler.GetStringValue(user.SUser_Name, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append(" SUser_Pwd='" + ValueHandler.GetStringValue(user.SUser_Pwd, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append(" State=1 , ");
                    sbSql.Append(" CreateTime=getdate(),");
                    sbSql.Append(" CreateMan=" + LinkService.GetCurrentUser().SUser_Id);
                    db.Open();
                    SqlHelper.ExecuteNonQuery(db.Transaction, CommandType.Text, sbSql.ToString());
                    user.SUser_Id = ValueHandler.GetIntNumberValue(SqlHelper.ExecuteScalar(db.Transaction, CommandType.Text, "select @@identity "));
                    db.Transaction.Commit();
                    LinkService.InsertLog(ESysLogType.Operation, "新增用户[" + ValueHandler.GetStringValue(user.SUser_Name, ValueHandler.CharHandlerType.IsRepChar) + "]！", EMenuList.用户管理);
                }
                else
                {
                    sbSql.Append(" UPDATE SysUser SET ");
                    sbSql.Append(" SUser_Name='" + ValueHandler.GetStringValue(user.SUser_Name, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append(" SUser_Pwd='" + ValueHandler.GetStringValue(user.SUser_Pwd, ValueHandler.CharHandlerType.IsRepChar) + "' ");
                    sbSql.Append(" WHERE SUser_Id="+user.SUser_Id);
                    db.Open();
                    SqlHelper.ExecuteNonQuery(db.Transaction, CommandType.Text, sbSql.ToString());
                    db.Transaction.Commit();
                    LinkService.InsertLog(ESysLogType.Operation, "修改用户[" + ValueHandler.GetStringValue(user.SUser_Name, ValueHandler.CharHandlerType.IsRepChar) + "]！", EMenuList.用户管理);
                }
                blRes = true;
            }
            catch (Exception ex)
            {
                db.Transaction.Rollback();
                LinkService.InsertLog(ESysLogType.Operation, "新增/修改用户出错！[" + ex.ToString() + "]", EMenuList.用户管理);

            }
            db.Close();
            return blRes;
        }
        public bool Delete(SysUser user)
        {
            DataBase db = new DataBase();
            bool blRes = false;
            try
            {
                StringBuilder sbSql = new StringBuilder();
                sbSql.Append(" UPDATE SysUser SET ");
                sbSql.Append(" State=0 ");
                sbSql.Append(" WHERE SUser_Id=" + user.SUser_Id);
                db.Open();
                SqlHelper.ExecuteNonQuery(db.Transaction, CommandType.Text, sbSql.ToString());
                db.Transaction.Commit();
                LinkService.InsertLog(ESysLogType.Operation, "删除用户[" + ValueHandler.GetStringValue(user.SUser_Name, ValueHandler.CharHandlerType.IsRepChar) + "]！", EMenuList.用户管理);
                blRes = true;
            }
            catch (Exception ex)
            {
                db.Transaction.Rollback();
                LinkService.InsertLog(ESysLogType.Operation, "删除用户出错！[" + ex.ToString() + "]", EMenuList.用户管理);
            }
            db.Close();
            return blRes;
            
        }

        /// <summary>
        /// true 存在
        /// </summary>
        /// <param name="SM"></param>
        /// <returns></returns>
        public bool HasExistsUserName(SysUser SM)
        {
            DataBase db = new DataBase();
            bool blRes = true;
            try
            {
                StringBuilder sbSql = new StringBuilder();
                sbSql.Append(" SELECT * FROM SysUser WHERE SUser_Name='" + ValueHandler.GetStringValue(SM.SUser_Name, ValueHandler.CharHandlerType.IsRepChar) + "' AND SUser_Id<>" + SM.SUser_Id + " AND State=1 ");                
                SqlHelper.FillDataset(db.Connection, CommandType.Text, sbSql.ToString(), db.Data, new string[] { "dtList" });
                if (db.Data.Tables[0].Rows.Count > 0)
                    blRes = true;
                else
                    blRes = false;

            }
            catch (Exception ex)
            {
            }
            db.Close();
            return blRes;
        }
    }
}
